﻿using System.Diagnostics;

namespace AutoDeploy.Server
{
    public class CmdHelper
    {
        internal static async Task<(int exitCode, string msg)> ExecuteAsync(string fileName, string arguments,CancellationToken token = default,int timeOut = 5000)
        {
            var processStartInfo = new ProcessStartInfo
            {
                FileName = fileName,
                RedirectStandardError = true,
                Arguments = arguments,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            using var process = new Process { StartInfo = processStartInfo };

            try
            {
                process.Start();

                //读取输出和错误信息 string output = await process.StandardOutput.ReadToEndAsync();  
                string error = await process.StandardError.ReadToEndAsync();
                //await process.WaitForExitAsync(token);
                process.WaitForExit(timeOut);
                
                return ( process.ExitCode, error );
            }
            catch (Exception ex)
            {
                return (-1,ex.Message);
            }
        }
    }
}
